package com.hxht.cmp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hxht.cmp.entity.Favorite;
import com.hxht.cmp.mapper.FavoriteMapper;
import com.hxht.cmp.pojo.FavoritePojo;
import com.hxht.cmp.pojo.FavoriteResult;
import com.hxht.cmp.service.FavoriteService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author alice on 2017/8/16 0016.
 * @version 1.0
 * @since 1.0
 */
@Service
public class FavoriteServiceImp implements FavoriteService {
    @Resource
    private FavoriteMapper favoriteMapper;

    @Override
    public Favorite selectFavorite(Favorite favorite) {
        return favoriteMapper.selectOne(favorite);
    }

    @Override
    public int insertFavorite(Favorite favorite) {
        return favoriteMapper.insert(favorite);
    }

    @Override
    public int deleteFavorite(Favorite favorite) {
        return favoriteMapper.delete(favorite);
    }

    @Override
    public Page<Favorite> selectFavorite(int start, int pageSize, String userUid) {
        Example example = new Example(Favorite.class);
        example.createCriteria().andCondition("user_uid = ", userUid);
        example.setOrderByClause("date_created desc");
        return PageHelper.startPage(start, pageSize).doSelectPage(() -> favoriteMapper.selectByExample(example));
    }

    public Page<FavoriteResult> pageFavorite(FavoritePojo favoritePojo) {
        return PageHelper.startPage(favoritePojo.getPage(), favoritePojo.getPageSize()).doSelectPage(() -> favoriteMapper.selectFavorite(favoritePojo));
    }

    @Override
    public List<Favorite> selectAllFavorite(String userUid) {
        Favorite favorite = new Favorite();
        favorite.setUserUid(userUid);
        return favoriteMapper.select(favorite);
    }
}
